/**
 * 
 */
package com.esiag.isidis.fi.pdsteam.main;

import java.io.FileInputStream;
import java.util.Properties;

import com.dao.MessageDAO;
import com.dao.impl.MessageDAOImpl;
import com.esiag.isidis.fi.pdsteam.entity.Machine;
import com.esiag.isidis.fi.pdsteam.service.ObserverReceiver;
import com.esiag.isidis.fi.pdsteam.service.ServerReceiver;
import com.rev.Message;
import com.rev.Source;
import com.rev.Sourcetype;

/**
 * @author Idrissi
 * 
 */
public class Main {

	void remplirDB() {
		// remplir la bd
		Sourcetype type1 = new Sourcetype(1, "Heat sensor", null);
		Sourcetype type11 = new Sourcetype(11, "Heat sensor", null);
		Sourcetype type2 = new Sourcetype(2, "Pression sensor", null);
		Sourcetype type21 = new Sourcetype(21, "Pression sensor", null);
		Sourcetype type3 = new Sourcetype(3, "Leak sensor", null);
		Sourcetype type31 = new Sourcetype(31, "Leak sensor", null);
		Sourcetype type4 = new Sourcetype(4, "Optical sensor", null);
		Sourcetype type41 = new Sourcetype(41, "Optical sensor", null);
		Sourcetype type5 = new Sourcetype(5, "Speed sensor", null);
		Sourcetype type51 = new Sourcetype(51, "Speed sensor", null);
		Sourcetype type6 = new Sourcetype(6, "Water sensor", null);
		Sourcetype type61 = new Sourcetype(61, "Water sensor", null);
		Source source1 = new Source(1, type1, "Heat sensor 1", null);
		Source source2 = new Source(2, type11, "Heat sensor 2", null);
		Source source3 = new Source(3, type2, "Pression sensor 1", null);
		Source source4 = new Source(4, type21, "Pression sensor 2", null);
		Source source5 = new Source(5, type3, "Leak sensor 1", null);
		Source source6 = new Source(6, type31, "Leak sensor 2", null);
		Source source7 = new Source(7, type4, "Optical sensor 1", null);
		Source source8 = new Source(8, type41, "Optical sensor 2", null);
		Source source9 = new Source(9, type5, "Speed sensor 1", null);
		Source source10 = new Source(10, type51, "Speed sensor 2", null);
		Source source11 = new Source(11, type6, "Water sensor 1", null);
		Message message1 = new Message(1, source1, null, "", "Heat alarm", null);
		Message message2 = new Message(2, source2, null, "", "Fire Alarm", null);
		Message message3 = new Message(3, source3, null, "",
				"High Pression Alarm", null);
		Message message4 = new Message(4, source3, null, "",
				"Low Pression Alarm", null);
		Message message5 = new Message(5, source4, null, "",
				"High Pression Alarm", null);
		Message message6 = new Message(6, source4, null, "",
				"Low Pression Alarm", null);
		Message message7 = new Message(7, source8, null, "", "Speed Limit",
				null);
		Message message8 = new Message(8, source9, null, "", "Speed Limit",
				null);
		Message message9 = new Message(9, source10, null, "", "Water Alarm",
				null);
		Message message10 = new Message(10, source11, null, "",
				"Alarm warning", null);
		MessageDAO messageDAO = new MessageDAOImpl();
		messageDAO.insert(message1);
		messageDAO.insert(message2);
		messageDAO.insert(message3);
		messageDAO.insert(message4);
		messageDAO.insert(message5);
		messageDAO.insert(message6);
		messageDAO.insert(message7);
		messageDAO.insert(message8);
		messageDAO.insert(message9);
		messageDAO.insert(message10);

	}

	/**
	 * 
	 */
	private void start(Machine machine) {

		remplirDB();

		// receive data
		ServerReceiver serverReceiver = new ServerReceiver();
		ObserverReceiver observerReceiver = new ObserverReceiver();
		serverReceiver.addObserver(observerReceiver);
		serverReceiver.startReceive(machine);
	}

	public Main() {

		try {
			Properties properties = new Properties();
			FileInputStream file = new FileInputStream(
					"C:\\Users\\DAOUDA\\Documents\\config.properties");

			properties.load(file);
			String ipAddress = properties.getProperty("ipAddress");
			String strPort = properties.getProperty("port");
			file.close();
			int port = Integer.valueOf(strPort);
			Machine machine = new Machine(ipAddress, port);
			start(machine);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		new Main();

	}

}
